package com.test.crawler.service.impl;

import cn.hutool.core.util.StrUtil;
import com.test.crawler.service.BatchImportProductService;
import com.test.crawler.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class BatchImportProductServiceImpl implements BatchImportProductService {
    @Override
    public Result batch(CrawlerUrl productSources) {
        try {
            //------start------
            CrawlerIn crawlerIn = new CrawlerIn();
            crawlerIn.setUrl(productSources.getUrls());
            crawlerIn.setSelenium(true);
            crawlerIn.setHeadless(false);

            MallProductDownloader tmallProductDownloader = new MallProductDownloader();
            List<MallProduct> result = tmallProductDownloader.execute(crawlerIn);
            if (CollectionUtils.isNotEmpty(result)) {
                for (MallProduct tmallProduct : result) {
                    try {
                        Map<String, Object> productMap = new HashMap<String, Object>();
                        Map<String, SkuCustomAttr> map = new LinkedHashMap<>();
                        //设置自定义属性
                        setSkuAttr(tmallProduct, map);
                        Collection<SkuCustomAttr> customAttrs = map.values();
                        String instructions = tmallProduct.getInstructions();
                        System.out.println(instructions);
                        if (StrUtil.isNotBlank(instructions)){
                            String[] split = instructions.split("【");
                            List<String> splits= Lists.newArrayList();
                            for (String s : split) {
                                splits.add("【"+s);
                            }
                            tmallProduct.setInstructionsList(splits);
                        }
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    }
                }
                //TODO 商品保存

            }
            log.info("ImportTmProductJob end...");
            return Result.succeed(result);
        } catch (Exception e) {
            log.error("exec ImportTmProductJob failed.", e);
        } finally {

        }
        return null;
    }


    private static void setSkuAttr(MallProduct tmallProduct, Map<String, SkuCustomAttr> map) {
        if (tmallProduct.getSkuInfos() != null) {
            tmallProduct.getSkuInfos().forEach(tmallSku -> {
                String skuKeys = tmallSku.getKeys();//版本:8GB 256GB;颜色分类:金色;套餐类型:官方标配;
                System.out.println(skuKeys);
                String[] skuKeyGroup = skuKeys.split(";");
                for (String skuKey : skuKeyGroup) {//颜色分类:金色;
                    String[] skuPropertyAndValue = skuKey.split(":");
                    String propery = skuPropertyAndValue[0];//颜色分类
                    String properyVal = skuPropertyAndValue[1];//金色
                    if (!map.containsKey(propery)) {//规格名称不存在
                        map.put(propery, new SkuCustomAttr(propery, properyVal, Math.random() + ""));//新建规格属性
                    } else {
                        SkuCustomAttr skuCustomAttr = map.get(propery);
                        if (!skuCustomAttr.containsPropertyVal(properyVal)) {//不存在当前规格属性
                            skuCustomAttr.addPropertyVal(properyVal);
                        }
                    }
                }
            });
            tmallProduct.setCustomAttr(map.values());
        }
    }
}

